package day23;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 *
 * UdpReceive2.java
 * @author wangbiao<br/>
 *         email:785497344@qq.com
 * @description 一直循环等待接收端
 * @since 1.0.0
 * @date 2013-7-6上午11:52:38
 *
 */
public class UdpReceive2 {

	//可以不关闭资源
	public static void main(String[] args) {
		DatagramSocket ds = null;
		try {
			ds = new DatagramSocket(10000);
			//因为receive方法是阻塞式方法，因此不会陷入死循环
			while (true) {
				byte[] buf = new byte[1024];
				DatagramPacket dp = new DatagramPacket(buf, buf.length);
				ds.receive(dp);
				String ip = dp.getAddress().getHostAddress();
				System.out.println(ip + "::"
						+ new String(dp.getData(), 0, dp.getLength()));
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
